package com.yoomiito.app.adapter.home.reommend;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.model.ViewType;
import com.yoomiito.app.model.bean.HomeChosenGoods;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.widget.CouponView;
import com.yoomiito.app.widget.RoundImageView;

import java.util.List;

public class HomeItemChosenGoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<ViewType<HomeChosenGoods>> mGoodsList;
    private OnItemClickListener mOnItemClickListener;

    public HomeItemChosenGoodsAdapter(Context context, List<ViewType<HomeChosenGoods>> goodsList) {
        this.mContext = context;
        this.mGoodsList = goodsList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (20 == i) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_more, viewGroup, false);
            return new HomeItemChosenGoodsAdapter.ItemViewHolder(view);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_goods_8, viewGroup, false);
            return new HomeItemChosenGoodsAdapter.ItemGoodsViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof HomeItemChosenGoodsAdapter.ItemGoodsViewHolder) {
            HomeItemChosenGoodsAdapter.ItemGoodsViewHolder holder = (HomeItemChosenGoodsAdapter.ItemGoodsViewHolder) viewHolder;
            RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
            lp.leftMargin = DensityUtil.dp2px(i == 0 ? 2 : 0);
            HomeChosenGoods goods = mGoodsList.get(i).getT();
            holder.goodsTitleTv.setText(goods.getTitle());
            ImageLoader.getInstance().into(mContext, goods.getImageUrl(), holder.goodsIv,R.drawable.goods_default);

            if (goods.getType() == 1){
                if (SpannableUtils.isEmptyOr0(goods.getCouponPrice())){
                    holder.couponView.setHeaderTextVisible(false);
                    holder.couponView.setText("特价精选");
                }else {
                    holder.couponView.setHeaderTextVisible(true);
                    holder.couponView.setHeaderText(goods.getType() == 1 ? "券" : "赚");
                    holder.couponView.setText("¥" + SpannableUtils.handlePrice(goods.getCouponPrice()));
                }
                holder.couponView.setColor(ResUtil.getColor(R.color.color_FF7D00),ResUtil.getColor(R.color.color_1AFF7D00));
                holder.couponView.setVisibility(View.VISIBLE);
            }else {
                holder.couponView.setHeaderText(goods.getType() == 1 ? "券" : "赚");
                holder.couponView.setText("¥" + SpannableUtils.handlePrice(goods.getCouponPrice()));
                holder.couponView.setColor(ResUtil.getColor(R.color.color_FF035B),ResUtil.getColor(R.color.color_1AFF035B));
                holder.couponView.setVisibility(SpannableUtils.isEmptyOr0(goods.getCouponPrice()) ? View.INVISIBLE : View.VISIBLE);
            }



            //1淘宝商品；2礼包商品；3爆品商品；4优品商品；5区代商品
            String p = SpannableUtils.handlePrice(goods.getDiscountPrice());
            String oldP = SpannableUtils.handlePrice(goods.getReservePrice());
            String price = (SpannableUtils.isEmptyOr0(goods.getReservePrice()) || goods.getDiscountPrice().equals(goods.getReservePrice())) ?
                    "¥" + p + " " : "¥" + p + " ¥" + oldP;
            int endP = price.indexOf(" ");
            SpannableString spanText = new SpannableString(price);
            spanText.setSpan(new RelativeSizeSpan(1.4f), 1, endP, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spanText.setSpan(new ForegroundColorSpan(ResUtil.getColor(R.color.color_text)), endP + 1, price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
            spanText.setSpan(strikethroughSpan, endP + 1, price.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            holder.goodsPriceTv.setText(spanText);


            holder.itemView.setOnClickListener(v -> {
                if (mOnItemClickListener != null) mOnItemClickListener.onClick(i, false);
            });
        } else {
            HomeItemChosenGoodsAdapter.ItemViewHolder holder = (HomeItemChosenGoodsAdapter.ItemViewHolder) viewHolder;
            holder.mLookMoreTv.setOnClickListener(v -> {
                if (mOnItemClickListener != null) mOnItemClickListener.onClick(-1, true);
            });
        }
    }

    @Override
    public int getItemCount() {
        return mGoodsList == null ? 0 : mGoodsList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mGoodsList.get(position).getViewType();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onClick(int position, boolean isLookMore);
    }

    private class ItemGoodsViewHolder extends RecyclerView.ViewHolder {
        private TextView goodsTitleTv;
        private CouponView couponView;
        private TextView goodsPriceTv;
        private RoundImageView goodsIv;
        private View itemView;

        public ItemGoodsViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView = itemView;
            goodsIv = itemView.findViewById(R.id.goodsIv);
            goodsTitleTv = itemView.findViewById(R.id.goodsTitle);
            couponView = itemView.findViewById(R.id.couponView);
            goodsPriceTv = itemView.findViewById(R.id.goodsPrice);
            itemView.findViewById(R.id.oldGoodsPrice).setVisibility(View.GONE);
        }
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView mLookMoreTv;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            mLookMoreTv = itemView.findViewById(R.id.item_look_more);
            GradientDrawable drawable = (GradientDrawable) mLookMoreTv.getBackground();
            drawable.setColor(ResUtil.getColor(R.color.color_FF246C));
        }
    }
}
